package com.book.controller;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.util.UriUtils;

import com.book.constant.Constants;
import com.book.formbean.BookForm;
import com.book.formbean.UserForm;
import com.book.model.Book;
import com.book.service.BookService;
import com.book.service.FtpService;

@Controller
public class BookController {
	@Autowired
	private BookService bookService;

	private static final Logger logger = LoggerFactory.getLogger(BookController.class);

	@RequestMapping(value = { "/" }, method = RequestMethod.GET)
	public String indexs(BookForm bookForm, Model model) throws Exception {
		initModel(model, bookForm);
		return "index";
	}

	@RequestMapping(value = { "search.do" }, method = RequestMethod.GET)
	public String search(BookForm bookForm, Model model) throws Exception {
		initModel(model, bookForm);
		return "result";
	}

	@RequestMapping(value = { "addPage.do" }, method = RequestMethod.POST)
	public String addBook(BookForm bookForm, Model model, HttpSession httpSession) throws Exception {
		bookService.addBook(bookForm);
		initModel(model, new BookForm());
		return "admin_index";
	}

	@RequestMapping(value = { "addPageInit.do" }, method = RequestMethod.GET)
	public String addPageInit(BookForm bookForm, Model model, HttpSession httpSession) throws Exception {
		if (httpSession.getAttribute("user") != null) {
			model.addAttribute("type", bookService.getType());
			model.addAttribute("bookForm", bookForm);
			return "addPage";
		} else {
			return "loginPage";
		}
	}

	@RequestMapping(value = { "del.do" }, method = RequestMethod.GET)
	public String delBook(Model model, BookForm bookForm, Book book) throws Exception {
		bookService.delBook(book);
		initModel(model, bookForm);
		return "admin_index";
	}

	@RequestMapping(value = { "login.do" }, method = RequestMethod.GET)
	public String login(Model model, HttpSession session, UserForm userForm, BookForm bookForm) throws Exception {
		if (bookService.isRightUser(userForm) || session.getAttribute("user") != null) {
			if (userForm.getUserName() != null) {
				session.setAttribute("user", userForm.getUserName());
			}
			initModel(model, bookForm);
			return "admin_index";
		} else {
			return "loginPage";
		}
	}

	@RequestMapping(value = { "download*" }, method = RequestMethod.GET)
	@ResponseBody
	public void download(HttpServletResponse response, Book book) throws Exception {
		String filename = book.getPath();
		String folderName = bookService.getFolderName(book.getId());
		response.setContentType("APPLICATION/OCTET-STREAM");
		response.setHeader("Content-Disposition", "attachment; filename=\"" + UriUtils.encodeQuery(filename, "utf-8") + "\"");
		boolean result = FtpService.downFile(Constants.FTP_URL, 21, Constants.FTP_USER, Constants.FTP_PASSWORD, folderName, filename, response.getOutputStream());
		logger.info("download file:" + filename + "===> " + result);
	}

	private void initModel(Model model, BookForm bookForm) {
		model.addAttribute("bookPage", bookService.findBookPage(bookForm));
		model.addAttribute("bookForm", bookForm);
		model.addAttribute("type", bookService.getType());
	}

	@RequestMapping(value = { "showContent*" }, method = RequestMethod.GET)
	@ResponseBody
	public void showContent(HttpServletResponse response, Book book) throws Exception {
		String filename = book.getPath();
		String folderName = bookService.getFolderName(book.getId());
		FtpService.showFileContent(Constants.FTP_URL, 21, Constants.FTP_USER, Constants.FTP_PASSWORD, folderName, filename, response.getOutputStream());
	}
}
